/*
 * SourceFile.h
 *
 *  Created on: Nov 19, 2010
 *      Author: dhovemey
 */

#ifndef SOURCEFILE_H_
#define SOURCEFILE_H_

#include <string>
class ASTNode;

class SourceFile {
private:
	std::string m_srcFileName;
	ASTNode *m_unitAST;
	int m_refCount;

	SourceFile(const SourceFile &);
	SourceFile& operator=(const SourceFile &);

public:
	SourceFile(const std::string &srcFileName, ASTNode *unitASTToAdopt);
	virtual ~SourceFile();

	std::string getSourceFileName() const { return m_srcFileName; }

	int getRefCount() const { return m_refCount; }
	void attach() { m_refCount++; }
	void detach() { m_refCount--; }
};

/**
 * Manage a temporary pointer to a SourceFile object.
 */
class SourceFilePtr {
private:
	SourceFile *m_srcFile;

	SourceFilePtr(const SourceFilePtr &);
	SourceFilePtr& operator=(const SourceFilePtr &);

public:
	SourceFilePtr(SourceFile *srcFile) {
		m_srcFile = srcFile;
		m_srcFile->attach();
	}

	~SourceFilePtr() {
		m_srcFile->detach();
		if (m_srcFile->getRefCount() == 0) {
			delete m_srcFile;
		}
	}

	SourceFile* get() { return m_srcFile; }
	SourceFile* operator->() { return m_srcFile; }
};

#endif /* SOURCEFILE_H_ */
